package com.studorm.xml.action;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.opensymphony.xwork2.ActionSupport;
import com.studorm.Connection_mysql;
import com.studorm.XMLOption;
import com.studorm.vo.Kuaijian;

public class KuaiJianAction  extends ActionSupport {
			
	/**
	 * 
	 */
	private static final long serialVersionUID = -3697921271744988026L;
	private List<Kuaijian> tableRow = new ArrayList<Kuaijian>();
	private int amountOfRow = 0;
	public List<Kuaijian> getTableRow() {
		return tableRow;
	}
	public void setTableRow(List<Kuaijian> tableRow) {
		this.tableRow = tableRow;
	}
	
	public String loadExpress() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String dormBdName = request.getParameter("dornBdName"); 
		String sign = request.getParameter("issignin");
		String rowEachP_t = request.getParameter("rowEachP");//每页行数
		String nuOfP_t = request.getParameter("nuOfP");//要获取第几页的数据
		int issignin;	
		if(sign.equals("已领取"))
			{
			issignin=1;
			}
		else 
			{
			issignin=0;
			}
		
		int rowEachP = 10; //每页行数
		int nuOfP = 1;//当前是第几页
		if(rowEachP_t != null){
			rowEachP = Integer.parseInt(rowEachP_t);
		}
		if(nuOfP_t != null){
			nuOfP = Integer.parseInt(nuOfP_t);
		}
		
	
		String dorminfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\dorminfs.xml");
		Document dorminfsDoc = XMLOption.loadXML(dorminfsPath);
		StringBuilder dormidCondition = null;
		
		List<? extends Node> list = dorminfsDoc.selectNodes("/dorminfs/dorminf[dormBdName='"+dormBdName+"']");
		Iterator<? extends Node> it = list.iterator();
		dormidCondition = new StringBuilder();		
		while(it.hasNext()){
			dormidCondition.append("dorm="+it.next().valueOf("@id"));
			if(it.hasNext()){
				dormidCondition.append(" or ");
			}
		}
		
		String studentsinfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\studentsinfs.xml");
		Document studentsinfsDoc = XMLOption.loadXML(studentsinfsPath);
		
		List<? extends Node> stuNulist = studentsinfsDoc.selectNodes("/studentsinfs/studentsinf["+dormidCondition.toString()+"]");
		Iterator<? extends Node> stuNuit = stuNulist.iterator();
		
		StringBuilder stuNuCondition=null;
		stuNuCondition = new StringBuilder();			
		while(stuNuit.hasNext()){
			stuNuCondition.append("recipients="+stuNuit.next().valueOf("@studentNu"));
			if(stuNuit.hasNext()){
				stuNuCondition.append(" or ");
			}
		}
		
		
		
		String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
		Document expressDoc = XMLOption.loadXML(expressPath);
		List<? extends Node> expresslist = expressDoc.selectNodes("/expresses/express[("+stuNuCondition.toString()+")"+"and issignin ="+issignin+"]");
		amountOfRow = expresslist.size();
		
		for(int count = (nuOfP-1) * rowEachP; count < (nuOfP-1) * rowEachP+rowEachP && count < expresslist.size(); ++count){
			Node n = expresslist.get(count);
		
			Kuaijian express = new Kuaijian();
			express.setDormBdName(dormBdName);	
			
			Node dornNode = studentsinfsDoc.selectSingleNode("studentsinfs/studentsinf[@studentNu="+n.valueOf("recipients")+"]/dorm");
			Node dornNuNode = dorminfsDoc.selectSingleNode("dorminfs/dorminf[@id="+dornNode.getText()+"]/dormNu");
			Node stuNaNode = studentsinfsDoc.selectSingleNode("studentsinfs/studentsinf[@studentNu="+n.valueOf("recipients")+"]/name");
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			java.util.Date arrival =  sdf.parse(n.valueOf("arrivalTime"));
			java.util.Date signtime;
			if(n.valueOf("signforTime").isEmpty())
			{
				signtime=null;
			}
			else {
				signtime = sdf.parse(n.valueOf("signforTime"));
			} 
			
			
			express.setDormNum(dornNuNode.getText());
			express.setStuNum(n.valueOf("recipients"));
			express.setStuName(stuNaNode.getText());
			express.setArrivalTime(arrival);
			express.setIssignin(issignin);
			express.setSignforTime(signtime);
			express.setId(Integer.parseInt(n.valueOf("@id")));
			tableRow.add(express);
		}
		
		
		return SUCCESS;
	}
	
	public String delExpress() throws Exception{
		//读取请求的参数 
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String expressId = request.getParameter("delId");
		int id = 0;
		if(expressId != null){
			id = Integer.parseInt(expressId);
		}else{
			return NONE;
		}
		
		String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
		Document expressDoc = XMLOption.loadXML(expressPath);
		Node delNode = expressDoc.selectSingleNode("/expresses/express[@id='"+expressId+"']");
		if(delNode == null){
			return NONE;
		}
		Boolean succ = delNode.getParent().remove(delNode);
		
		if(succ){
			XMLOption.writeXml(expressDoc, expressPath);
			return SUCCESS;
		}else{
			return NONE;
		}
	}
	
	public String alterSign() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String id = request.getParameter("alterId");
		String status = request.getParameter("status");
		
		//获取当前系统时间
		java.util.Date time = new java.util.Date();
		java.sql.Date signfortime = new java.sql.Date(time.getTime());
		
		//规范时间日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String datetime = sdf.format(signfortime); 
		
		
		String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
		Document expressDoc = XMLOption.loadXML(expressPath);
		Node issignNode = expressDoc.selectSingleNode("/expresses/express[@id='"+id+"']/issignin");
		Node signtimeNode = expressDoc.selectSingleNode("/expresses/express[@id='"+id+"']/signforTime");
		
		if(id == null || id.isEmpty()){
			return NONE;
		}

	
		if(status.equals("未领取")){
	        issignNode.setText("1");
	        signtimeNode.setText(datetime);
		}else if(status.equals("已领取")){
			issignNode.setText("0");
			datetime="";
			signtimeNode.setText(datetime);
		}else
		{
			return NONE;
		}
		
		XMLOption.writeXml(expressDoc, expressPath);

		return SUCCESS;

	}
	
public String addExpress() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String stuID = request.getParameter("stuID");
		String sign = request.getParameter("sign");
		String arrival = request.getParameter("arrivaltime");
		String signtime = request.getParameter("signtime");
		
		int issign;
		if(sign.equals("未领取"))
		{
			issign=0;
		}
		else {
			issign=1;
		}
		
		//Pattern patternNu = Pattern.compile("\\d{3}");// 匹配3位整数
		//Pattern patternDeci = Pattern.compile("\\d*\\.{0,1}\\d*"); // 匹配小数或整数
		//Pattern patternYM = Pattern.compile("[1-2]\\d*-[0-1]{0,1}[0-9]");// 匹配 ????-??的年月格式
		
		if(stuID.isEmpty()){
			return NONE;
		}
		if(sign == null || sign.isEmpty() ){
			return NONE;
		}
		if(arrival == null || arrival.isEmpty()){
			return NONE;
		}
	
		
		arrival=arrival+":00.0";
		String time[] = arrival.split("T");
		String arrivaltime=time[0]+" "+time[1];
		String signfortime;
		if(signtime.isEmpty())
		{
			signfortime ="";
		}
		else {
			signtime=signtime+":00.0";
			String time2[]= signtime.split("T");
			signfortime=time2[0]+" "+time2[1];
		}
		
		String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
		Document expressDoc = XMLOption.loadXML(expressPath);
		
		Node countNode = expressDoc.selectSingleNode("/expresses/count");
		String  count = countNode.getText();
		countNode.setText(String.valueOf(Integer.parseInt(count)+1));
		
		Element expresses = expressDoc.getRootElement();
		Element express = expresses.addElement("express");
		express.addAttribute("id", count);
		
		Element recipients = express.addElement("recipients");
		recipients.setText(stuID);
		
		Element arrivalTime = express.addElement("arrivalTime");
		arrivalTime.setText(arrivaltime);

	
		Element signforTime = express.addElement("signforTime");
		signforTime.setText(signfortime);
		
		Element issignin = express.addElement("issignin");
		issignin.setText(String.valueOf(issign));
		
			
		// 保存
		XMLOption.writeXml(expressDoc, expressPath);
		
		return SUCCESS;
	}
	
public String editExpress() throws Exception{
	
	HttpServletRequest request = ServletActionContext.getRequest(); 
	String stuID = request.getParameter("stuID");
	String sign = request.getParameter("sign");
	String signtime = request.getParameter("signtime");
	String arrival = request.getParameter("arrivaltime");
	String id = request.getParameter("Expid");
	int issign;
	if(sign.equals("未领取"))
	{
		issign=0;
	}
	else {
		issign=1;
	}

	
	Pattern patternNu = Pattern.compile("\\d*");
	//Pattern patternDeci = Pattern.compile("\\d*\\.{0,1}\\d*"); // 匹配小数或整数
	//Pattern patternYM = Pattern.compile("[1-2]\\d*-[0-1]{0,1}[0-9]");// 匹配 ????-??的年月格式
	
	if(id == null || id.isEmpty() || !patternNu.matcher(id).matches()){
		return NONE;
	}

	if(arrival == null || arrival.isEmpty()){
		return NONE;
	}
	if(sign == null || sign.isEmpty()){
		return NONE;
	}
	
	arrival=arrival+":00.0";
	String time[] = arrival.split("T");
	String arrivaltime=time[0]+" "+time[1];
	String signfortime;
	if(signtime.isEmpty())
	{
		signfortime ="";
	}
	else {
		signtime=signtime+":00.0";
		String time2[]= signtime.split("T");
		signfortime=time2[0]+" "+time2[1];
	}

	String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
	Document expressDoc = XMLOption.loadXML(expressPath);
	Node expressNode = expressDoc.selectSingleNode("/expresses/express[@id="+id+"]");
	
	expressNode.selectSingleNode("arrivalTime").setText(arrivaltime);
	expressNode.selectSingleNode("signforTime").setText(signfortime);
	expressNode.selectSingleNode("issignin").setText(String.valueOf(issign));
	
	XMLOption.writeXml(expressDoc, expressPath);
	
		return SUCCESS;
}


public String loadAExpress() throws Exception{
	HttpServletRequest request = ServletActionContext.getRequest(); 
	String ExpId = request.getParameter("editId");
	Pattern patternNu = Pattern.compile("\\d*");
	if(ExpId == null || ExpId.isEmpty() || !patternNu.matcher(ExpId).matches()){
		return NONE;
	}
	
	String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
	Document expressDoc = XMLOption.loadXML(expressPath);
	Node expressNode = expressDoc.selectSingleNode("/expresses/express[@id="+ExpId+"]");
	
	
		Kuaijian express = new Kuaijian();
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date arrival =  sdf.parse(expressNode.valueOf("arrivalTime"));
		java.util.Date signtime;
		if(expressNode.valueOf("signforTime").isEmpty())
		{
			signtime=null;
		}
		else {
			signtime = sdf.parse(expressNode.valueOf("signforTime"));
		} 

		express.setStuNum(expressNode.valueOf("recipients"));
		express.setArrivalTime(arrival);
		express.setSignforTime(signtime);
		express.setIssignin(Integer.parseInt(expressNode.valueOf("issignin")));
		express.setId(Integer.parseInt(ExpId));
			
		tableRow.add(express);

	return SUCCESS;
}


public String loadStuExpress() throws Exception
{
	HttpServletRequest request = ServletActionContext.getRequest(); 
	HttpSession session = request.getSession();
	String id = (String)session.getAttribute("id");
	String sign = request.getParameter("issignin");	
	String rowEachP_t = request.getParameter("rowEachP");//每页行数
	String nuOfP_t = request.getParameter("nuOfP");//要获取
	int issign;
	int rowEachP = 10; //每页行数
	int nuOfP = 1;//当前是第几
	if(id == null ||id.isEmpty()){		
		return NONE;
	}
	if(sign.equals("未领取"))
	{
		issign = 0;
	}
	else {
		issign=1;
	}

	if(rowEachP_t != null){
		rowEachP = Integer.parseInt(rowEachP_t);
	}
	if(nuOfP_t != null){
		nuOfP = Integer.parseInt(nuOfP_t);
	}
	
	String studentsinfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\studentsinfs.xml");
	Document studentsinfsDoc = XMLOption.loadXML(studentsinfsPath);
		
	
	String expressPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\express.xml");
	Document expressDoc = XMLOption.loadXML(expressPath);

	List<? extends Node> expresslist = expressDoc.selectNodes("/expresses/express[(recipients ="+id+") and issignin ="+issign+"]");
	amountOfRow = expresslist.size();
	
	Node stuNaNode = studentsinfsDoc.selectSingleNode("studentsinfs/studentsinf[@studentNu="+id+"]/name");
	
	for(int count = (nuOfP-1) * rowEachP; count < (nuOfP-1) * rowEachP+rowEachP && count < expresslist.size(); ++count){
		Node n = expresslist.get(count);
	
		Kuaijian express = new Kuaijian();
		
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date arrival =  sdf.parse(n.valueOf("arrivalTime"));
		java.util.Date signtime;
		if(n.valueOf("signforTime").isEmpty())
		{
			signtime=null;
		}
		else {
			signtime = sdf.parse(n.valueOf("signforTime"));
		} 
	
		express.setStuNum(id);
		express.setArrivalTime(arrival);
		express.setSignforTime(signtime);
		express.setIssignin(Integer.parseInt(n.valueOf("issignin")));
		express.setStuName(stuNaNode.getText());	
		tableRow.add(express);

	}	
	return SUCCESS;
}



	public int getAmountOfRow() {
		return amountOfRow;
	}
	public void setAmountOfRow(int amountOfRow) {
		this.amountOfRow = amountOfRow;
	}
}
